<?php
defined('IN_PHPCMS') or exit('No permission resources.');

pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);
class company  {
	private $db,$userid;
	private $comapny_db,$signed_db;
	private $buy_product_db; 
	private $company_data;
	private $messages_db;
	private $content_db;
	function __construct() {
		//取得进来的查看的会员信息
		$phpcms_auth = param::get_cookie('auth');
		$auth_key = md5(pc_base::load_config('system', 'auth_key').str_replace('7.0' ,'8.0',$_SERVER['HTTP_USER_AGENT']));
		list($userid, $password)= explode("\t", sys_auth($phpcms_auth, 'DECODE', $auth_key));
		$this->userdb = pc_base::load_model('member_model');
		$this->memberinfo = $this->userdb->get_one(array('userid'=>$userid));
		
		//取得公司用户副表信息
		$userid = intval($_GET['userid'])>0 ? intval($_GET['userid']) : $_POST['userid'];
		if($userid<1) {return ;}
		
		$this->comapny_db = pc_base::load_model('company_model');
		$this->member_db = pc_base::load_model('member_model');
		
		$this->company_data = $this->comapny_db->get_one('userid='.$userid);
		$this->userid = $this->company_data['userid'];
		//取得公司用户主表信息
		$this->comapny_user = $this->member_db->get_one('userid='.$this->userid);
		
		$this->db = pc_base::load_model('content_model');
	}
	//首页
	public function init() {
		
		if(isset($_GET['siteid'])) {
			$siteid = intval($_GET['siteid']);
		} else {
			$siteid = 1;
		}
		$siteid = $GLOBALS['siteid'] = max($siteid,1);
		//SEO
		$SEO = seo($siteid);
		$sitelist  = getcache('sitelist','commons');
		$default_style = $sitelist[$siteid]['default_style'];
		$CATEGORYS = getcache('category_content_'.$siteid,'commons');
		include template('content','index',$default_style);
	}
	//公司企业首页
	function company(){
		$cityid  = $_SESSION['cityid'];
		include template('company','company');
	}
	//设计师首页
	function designer(){
		include template('designer','designer');
	}
	//单个公司首页
	function company_index(){
		$c_data = $this->company_data;	
		$username = $this->comapny_user['username'];
		include template('company','company_index');
	}
	//公司网上签约页面
	function signed(){
		if(isset($_POST['dosubmit'])){
			//print_r($this->company_data);exit;
			$memberinfo = $this->memberinfo;
			$userid = $memberinfo['userid'];
			$typeid = $memberinfo['typeid'];
			if($typeid!=1 || $userid==''){
				showmessage('请注册业会员后在签约',HTTP_REFERER);
				exit;
			}
			$signed_data = array(
				'c_userid'=>$this->company_data['userid'],//公司的用户ID
				'project_name'=>$_POST['project_name'],
				'name'=>$_POST['name'],
				'contact'=>$_POST['contact'],
				'userid'=>$userid,//业主的用户ID
				'typeid'=>$typeid,
				'usertypeid'=>$this->comapny_user['typeid'], //签约的公司或者是设计师
				'addtime'=>strtotime("now")
			);
			$this->signed_db = pc_base::load_model('signed_model');
			$this->signed_db->insert($signed_data);
			showmessage('签约成功',HTTP_REFERER);
		}
		if($this->memberinfo['typeid']!=1){
			showmessage('只有业主用户才有权限查看此页面',HTTP_REFERER);
		}
		$c_data = $this->company_data;
		$time = date('Y-m-d H:i:s');
		
		include template('company','signed');
	}
	//公司简介
	function about_us(){
		$c_data = $this->company_data;
		include template('company','about_us');
	}
	//公司案例
	function c_case(){
		$c_data = $this->company_data;
		$username = $this->comapny_user['username'];
		include template('company','case');
	}
	function case_detail()
	{
		$C_data = $this->designer_data;

		$username = $this->designer_user['username'];
		$this->db = pc_base::load_model('content_model');
		$id = intval($_GET['id']);
		$tablename = $this->db->table_name = $this->db->db_tablepre.'company_case';
		$r = $this->db->get_one('id='.$_GET['id']);

		$this->db->table_name = $tablename.'_data';
		$r2 = $this->db->get_one(array('id'=>$id));
		$rs = $r2 ? array_merge($r,$r2) : $r;
		$photos = $rs['case_photo'];
	
		if($photos == '') return array();
		eval("\$arr = $photos;");
	
		//print_r($rs);exit;
	
		include template('company','case_detail');
		
	}
	//公司公告
	function notice(){
		$c_data = $this->company_data;
		include template('company','notice');
	}
	//公司公告
	function notice_detail(){
		$c_data = $this->company_data;
		$id = intval($_GET['id']);
		$this->content_db = pc_base::load_model('content_model');
		$tablename = $this->db->table_name = $this->db->db_tablepre.'discount_notice';
		$r = $this->content_db->get_one(array('id'=>$id));
	
		if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
		
		$this->content_db->table_name = $tablename.'_data';
		$r2 = $this->db->get_one(array('id'=>$id));
		$rs = $r2 ? array_merge($r,$r2) : $r;
		include template('company','notice_detail');
	}
	//公司装饰杂谈
	function zatan(){
		$c_data = $this->company_data;
		$username = $this->comapny_user['username'];
		include template('company','zatan');
	}
	//公司装饰杂谈
	function zatan_detail(){
		$c_data = $this->company_data;
		$id = intval($_GET['id']);
		$this->db = pc_base::load_model('content_model');
		$tablename = $this->db->table_name = $this->db->db_tablepre.'decoration_topic';
		$r = $this->db->get_one(array('id'=>$id));
	
		if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
		
		$this->content_db->table_name = $tablename.'_data';
		$r2 = $this->db->get_one(array('id'=>$id));
		$rs = $r2 ? array_merge($r,$r2) : $r;
		include template('company','zatan_detail');
	}
	//留言咨询
	function message(){
		
		$this->messages_db = pc_base::load_model('messages_model');
		if(isset($_POST['dosubmit'])){
			$this->_session_start();
			if($_SESSION['code']!=$_POST['code']){
				showmessage('验证码输入不正确',HTTP_REFERER);
			}
			
			$memberinfo = $this->memberinfo;
			$userid = $memberinfo['userid'];
			$message_data = array(
			
				'content'=>$_POST['content'],
				'name'=>$_POST['name'],
				'phone'=>$_POST['phone'],
				'userid'=>$_POST['userid'],
				'post_userid'=>$this->memberinfo['userid'],
				'addtime'=>strtotime("now")
			);
			//print_r($message_data);exit;
			$this->messages_db->insert($message_data);
			showmessage('你的提问已成功请交，我们会尽快复您的问题',HTTP_REFERER);
		}
		$table_pre = $this->messages_db->db_tablepre;
		$member_company_db = $table_pre.'member_company';
		$message_db = $table_pre.'messages';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
		$memberinfo = $this->memberinfo;
		if($_GET['messageid'] && $this->company_data['userid']==$memberinfo['userid']){
			$con = ' and a.messageid='.$_GET['messageid'];
		}
		$sql = 'SELECT a.content,a.addtime,a.messageid,a.replyid,a.name, b.content as rcontent,b.replyid as rid 
FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_company_db.' AS c ON c.userid = a.userid
WHERE a.userid ='.intval($_GET['userid']).$con.' order by a.messageid desc ';

		$sql_count = 'SELECT  count(a.messageid) as num 
FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_company_db.' AS c ON c.userid = a.userid
WHERE a.userid ='.intval($_GET['userid']).$con;
		
		$sql .= " LIMIT $offset,$pagesize ";
		//echo $sql;
		$this->messages_db->query($sql);
		$message_data = $this->messages_db->fetch_array();
		$this->messages_db->query($sql_count);
		$total = $this->messages_db->fetch_array();
		
		$total = $total[0]['num'];
		$memberinfo = $this->memberinfo;
		
		$reply = $this->company_data['userid']==$memberinfo['userid'] ? 1:0;
			
		$pages2 = pages($total, $page, $pagesize);
		//echo '<pre>';
		//print_r($message_data);exit;
		$c_data = $this->company_data;
		include template('company','message');
	}
	//公司回复留言
	function reply_message(){
		$this->messages_db = pc_base::load_model('messages_model');
		if(isset($_POST['dosubmit'])){
			$memberinfo = $this->memberinfo;
			$userid = $memberinfo['userid'];
			$message_data = array(
				'content'=>$_POST['content'],
				'userid'=>$userid,
				'replyid'=>$_POST['messageid'],
				'addtime'=>strtotime("now")
			);
			$result = $this->messages_db->insert($message_data);
			if($result==1){
				$r = array('id'=>$_POST['messageid'], 'content'=>$_POST['content']);
				echo json_encode($r); 
			}else{
				echo 0;
			}
		}
	}
	
	/**
	 * 
	 * 删除留言
	 */
	function del_message(){
		$messageid = intval($_GET['messageid']);
		if($messageid<1) return ;
		//echo $messageid;exit;
		$this->messages_db = pc_base::load_model('messages_model');
		$this->messages_db->delete('messageid='.$messageid);
		$this->messages_db->delete('replyid='.$messageid);
		showmessage('删除成功',HTTP_REFERER); 
	}
	//公司评价
	function appraise(){
		$this->signed_db = pc_base::load_model('signed_model');
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
	
		$sql = 'SELECT *  FROM '.$this->signed_db->table_name.' WHERE c_userid='.$this->userid.' and inputtime <> \'0\' order by signedid desc ';

		$sql_count = 'select count(signedid) as num FROM '.$this->signed_db->table_name.' WHERE c_userid='.$this->userid.' and inputtime  <> \'0\' order by signedid desc ';
		
		$sql .= " LIMIT $offset,$pagesize ";
		
		$this->signed_db->query($sql);
		$pj_data = $this->signed_db->fetch_array();
		//print_r($pj_data);exit;
		$this->signed_db->query($sql_count);
		$total = $this->signed_db->fetch_array();
		$total = $total[0]['num'];
		$pagesbar = pages($total, $page, $pagesize);
		
		$c_data = $this->company_data;
		include template('company','appraise');
	}
	//公司联系方式
	function contact(){
		$c_data = $this->company_data;
		include template('company','contact');
	}
	private function _session_start() {
		$session_storage = 'session_'.pc_base::load_config('system','session_storage');
		pc_base::load_sys_class($session_storage);
	}
}
?>